
import java.io.IOException;
import java.io.InputStream;
import javax.microedition.media.*;
import javax.microedition.media.control.VolumeControl;

public class Game {

    private Players black;
    private Players white;
    private int state;
    private boolean turn;
    private Player player;
    private Player backGroundPlayer;

    //constructor
    public Game(String playerOneName, String playerTwoName) {
        state = AllDef.READY;
        turn = AllDef.BLACK;
        black = new Players(playerOneName, AllDef.BLACK);
        white = new Players(playerTwoName, AllDef.WHITE);
    }

    public Players getBlack() {
        return black;
    }

    public boolean getTurn() {
        return turn;
    }

    public void setTurn(boolean turn) {
        this.turn = turn;
    }

    public int getState() {
        return state;
    }

    public void setState(int state) {
        this.state = state;
    }

    public Players getWhite() {
        return white;
    }

    //play sound
    public void playSound(String src, boolean soundOn) {
        if (soundOn == true) {
            InputStream in = getClass().getResourceAsStream(src);
            try {
                player = Manager.createPlayer(in, "audio/x-wav");
                player.start();
            } catch (IOException ex) {
                ex.printStackTrace();
            } catch (MediaException ex) {
                ex.printStackTrace();
            } catch (IllegalArgumentException ex) {
                System.out.println(ex.toString());
            }
        } else {
            if (player != null) {
                player.close();
            }
        }
    }

    //play background music
    public void playBackGroundSound(String src, boolean soundOn) {
        if (soundOn == true) {
            InputStream in = getClass().getResourceAsStream(src);
            try {
                backGroundPlayer = Manager.createPlayer(in, "audio/midi");
                backGroundPlayer.prefetch();
                VolumeControl vc = (VolumeControl) backGroundPlayer.getControl("VolumeControl");
                vc.setLevel(40);
                backGroundPlayer.setLoopCount(-1);
                backGroundPlayer.start();
            } catch (IOException ex) {
                ex.printStackTrace();
            } catch (MediaException ex) {
                ex.printStackTrace();
            } catch (IllegalArgumentException ex) {
                System.out.println(ex.toString());
            }
        } else {
            if (backGroundPlayer != null) {
                backGroundPlayer.close();
            }
        }
    }

    public void stopMusic() {
        if (player != null) {
            player.close();
        }
    }

    public void stopBackGroundMusic() {
        if (backGroundPlayer != null) {
            backGroundPlayer.close();
        }
    }
}
